/**
* All Rights Reserved , Copyright (C) 2022 , 青岛鼎信通讯股份有限公司
*
* MpmProjectBidFullScoreService
* 评标细则总分表
*
* 修改纪录
* 2022-05-13 版本：1.0 hudeyong 创建。
* @version 版本：1.0
* @author 作者：hudeyong
* 创建日期：2022-05-13
*/
package com.topscomm.mpm.service.impl;

import com.topscomm.basic.exception.ServiceException;
import com.topscomm.basic.util.PrimaryKeyUtil;
import com.topscomm.mpm.pojo.MpmProjectBidFullScoreEntity;
import com.topscomm.mpm.service.IMpmProjectBidFullScoreService;
import com.topscomm.mpm.service.impl.auto.MpmProjectBidFullScoreServiceAuto;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(value = "mpmProjectBidFullScoreService")
@Scope(value = "singleton")
public class MpmProjectBidFullScoreService extends MpmProjectBidFullScoreServiceAuto implements IMpmProjectBidFullScoreService {

	@Override
	@Transactional
	public void doSave(Map<String, Object> map) {
		// TODO Auto-generated method stub
		for(int i = 1;i<=4;i++) {
			this.saveFullScore((Map<String, Object>) map.get("scoreMap"+i));
		}
	}

	public void saveFullScore(Map<String, Object> map) {
		// TODO Auto-generated method stub
		StringBufferProxy sql = new StringBufferProxy();
		MpmProjectBidFullScoreEntity fullScoreEntity = new MpmProjectBidFullScoreEntity();
		fullScoreEntity.convertFromMap(map);
		sql.appendSingle("type = '{0}' and times = '{1}' and projectcode = '{2}'",
				fullScoreEntity.getType(),fullScoreEntity.getTimes(),fullScoreEntity.getProjectcode());
		List<MpmProjectBidFullScoreEntity> fullScoreEntities = this.queryByWhere(sql.toString());
		if(fullScoreEntities.size()==0) {
			long[] fullScoreIds = PrimaryKeyUtil.GeneIDs(MpmProjectBidFullScoreEntity.tableName, 1);
			long id = fullScoreIds[0];
			fullScoreEntity.setId(id);
			// 第一次不需要添加
			if (!"1".equals(fullScoreEntity.getTimes())) {
				fullScoreEntity.setRequirescore("90"); //默认90分
			}
			this.insert(fullScoreEntity);
		} else {
			MpmProjectBidFullScoreEntity old = fullScoreEntities.get(0);
			old.setTotalfullsocre(fullScoreEntity.getTotalfullsocre());
			this.updateEntity(old);
		}
	}

	@Override
	public List<MpmProjectBidFullScoreEntity> queryByTime(String projectCode, String time){
		if (StringUtil.isEmpty(projectCode) || StringUtil.isEmpty(time)) {
			return new ArrayList<>();
		}
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" {0} = '{1}' ", MpmProjectBidFullScoreEntity.FieldProjectcode, projectCode);
		sql.appendSingle(" and {0} = '{1}' ", MpmProjectBidFullScoreEntity.FieldTimes, time);
		return queryByWhere(sql.toString());
	}
}
